Exploring LLVM's Build System Features
- build directives
- in tree/out of tree development
- in tree: contribute code directly to llvm project.
- out of tree: creating llvm extensions.
LLVM's CMake directives
Adding New Libraries
- Don't use normal
add_library. - Hard to control shared/static build of libraries using built in
add_library. - Better control over RTTI and exception handling if not using built in directives.
- Use
add_llvm_component_library. - Example:
add_llvm_component_library(<library name>
<source file>) - Instead of using
target_link_librariesandadd_dependencies, useLINK_COMPONENTSargument inadd_llvm_component_library. - Example:
add_llvm_component_library(<library name>
<source file>
LINK_COMPONENTS
<library 1> <library 2>) - Can use
LLVM_LINK_COMPONENTSvariable as well. - Example:
set(LLVM_LINK_COMPONENTS
<library 1> <library 2>)
add_llvm_component_library(...) - Component libraries are llvm building blocks. Different than real library names.
- Use
llvm_map_components_to_libnamesto create mapping from component library name to real one. - Use
LINK_LIBSargument ofadd_llvm_component_libraryto link against normal lib. - Use
DEPENDSargument to assign dependencies (instead ofadd_dependencies).
Add one build target per folder
- Source file detection problem with LLVM Cmake functions.
- LLVM build enforces all C/C++ source files in the same folder are added to the same lib, executable or plugin.
- Use
add_subdirectoryto add sub-folders and separate out different libs with separateCMakeLists.txt.
Adding executables and tools
- Use
add_llvm_executableoradd_llvm_tool. - Targets with
add_llvm_toolwill be added in installations. LLVM_BUILD_TOOLSenables/disables LLVM tool targets.- Can use
DEPENDS.
Adding Pass plugins
- Use
add_llvm_pass_plugin. LINK_COMPONENTS,LINK_LIBS, andDEPENDSavailable.
CMake integration for OOT project
- Two ways to create OOT project:
- llvm-config tool
- LLVM CMake modules
- Standard CMake package interface.
- Use
find_package(LLVM REQUIRED CONFIG)to import configs. ${LLVM_INCLUDE_DIRS}and${LLVM_LIBRARY_DIRS}forinclude_directoriesandlink_directories, resolves include and library path.- Use
-DLLVM_DIR=<llvm install path>/lib/cmake/llvmto find package. - Import
AddLLVMto get LLVM CMake functions. - Snippet:
list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR})
include(AddLLVM) - Need to take LLVM definitions into account.
add_definitions(${LLVM_DEFINITIONS}).- LLVM not built with RTTI, but non-llvm applications are. Make sure no RTTI mismatch.